package com.zbd.microservices.domain;

import java.io.Serializable;
import java.util.Collection;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import javax.persistence.*;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.oauth2.provider.ClientDetails;

/**
 * The persistent class for the oauth_client_details database table.
 * 
 */
@Entity
@Table(name = "oauth_client_details")
public class OauthClientDetail implements ClientDetails {
	private static final long serialVersionUID = 1L;

	@Id
	@Column(name = "client_id")
	private String clientId;

	@Column(name = "access_token_validity")
	private int accessTokenValidity;

	@Column(name = "additional_information")
	private String additionalInformation;

	private String authorities;

	@Column(name = "authorized_grant_types")
	private String authorizedGrantTypes;

	private String autoapprove;

	@Column(name = "client_secret")
	private String clientSecret;

	@Column(name = "refresh_token_validity")
	private int refreshTokenValidity;

	@Column(name = "resource_ids")
	private String resourceIds;

	private String scope;

	@Column(name = "web_server_redirect_uri")
	private String webServerRedirectUri;

	public OauthClientDetail() {
	}

	public String getClientId() {
		return this.clientId;
	}

	public void setClientId(String clientId) {
		this.clientId = clientId;
	}

	public int getAccessTokenValidity() {
		return this.accessTokenValidity;
	}

	public void setAccessTokenValidity(int accessTokenValidity) {
		this.accessTokenValidity = accessTokenValidity;
	}

	public void setAdditionalInformation(String additionalInformation) {
		this.additionalInformation = additionalInformation;
	}

	public void setAuthorities(String authorities) {
		this.authorities = authorities;
	}

	public void setAuthorizedGrantTypes(String authorizedGrantTypes) {
		this.authorizedGrantTypes = authorizedGrantTypes;
	}

	public String getAutoapprove() {
		return this.autoapprove;
	}

	public void setAutoapprove(String autoapprove) {
		this.autoapprove = autoapprove;
	}

	public String getClientSecret() {
		return this.clientSecret;
	}

	public void setClientSecret(String clientSecret) {
		this.clientSecret = clientSecret;
	}

	public int getRefreshTokenValidity() {
		return this.refreshTokenValidity;
	}

	public void setRefreshTokenValidity(int refreshTokenValidity) {
		this.refreshTokenValidity = refreshTokenValidity;
	}

	public void setResourceIds(String resourceIds) {
		this.resourceIds = resourceIds;
	}

	public void setScope(String scope) {
		this.scope = scope;
	}

	public String getWebServerRedirectUri() {
		return this.webServerRedirectUri;
	}

	public void setWebServerRedirectUri(String webServerRedirectUri) {
		this.webServerRedirectUri = webServerRedirectUri;
	}

	@Override
	public boolean isSecretRequired() {
		return true;// 肯定需要密码的
	}

	@Override
	public boolean isScoped() {
		return true;// 简单就是美
	}

	@Override
	public Set<String> getScope() {
		String[] scopeArray = this.scope.split(",");
		Set<String> scopes = new HashSet<String>(scopeArray.length);
		CollectionUtils.addAll(scopes, scopeArray);
		return scopes;
	}

	@Override
	public Set<String> getAuthorizedGrantTypes() {
		Set<String> authorizedGrantTypes = new HashSet<String>();
		if (StringUtils.isNoneBlank(this.authorizedGrantTypes)) {
			String[] authorizedGrantTypesArray = this.authorizedGrantTypes.split(",");
		
			CollectionUtils.addAll(authorizedGrantTypes, authorizedGrantTypesArray);
			return authorizedGrantTypes;
		} else
			return authorizedGrantTypes;
	}

	@Override
	public Set<String> getRegisteredRedirectUri() {
		Set<String> webServerRedirectUris = new HashSet<String>();
		if (StringUtils.isNoneBlank(webServerRedirectUri)) {
			String[] webServerRedirectUriArray = this.webServerRedirectUri.split(",");
			
			CollectionUtils.addAll(webServerRedirectUris, webServerRedirectUriArray);
			return webServerRedirectUris;
		} else
			return webServerRedirectUris;
	}

	@Override
	public Collection<GrantedAuthority> getAuthorities() {
		Collection<GrantedAuthority> grantedAuthoritys = new HashSet<GrantedAuthority>();
		if (StringUtils.isNoneBlank(authorities)) {
			String[] grantedAuthorityArray = this.authorities.split(",");
			CollectionUtils.addAll(grantedAuthoritys, grantedAuthorityArray);
			return grantedAuthoritys;
		} else
			return grantedAuthoritys;
	}

	@Override
	public Integer getAccessTokenValiditySeconds() {
		return this.accessTokenValidity;
	}

	@Override
	public Integer getRefreshTokenValiditySeconds() {
		return this.refreshTokenValidity;
	}

	@Override
	public boolean isAutoApprove(String scope) {
		if (this.autoapprove.equals("true"))
			return true;
		else
			return false;

	}

	@Override
	public Map<String, Object> getAdditionalInformation() {
		return null;// 放弃这个字段
	}

	@Override
	public Set<String> getResourceIds() {
		if (StringUtils.isNoneBlank(resourceIds)) {
			String[] resources = this.resourceIds.split(",");
			Set<String> set = new HashSet<String>(resources.length);
			CollectionUtils.addAll(set, resources);
			return set;
		} else
			return null;
	}

}